﻿namespace Timo.Common.Extension;

/// <summary>
/// 拓展系统函数
/// </summary>
public static class ConvertHelper
{
    /// <summary>
    /// 拓展Parse类型转换函数
    /// </summary>
    /// <typeparam name="T">需要转换的类型</typeparam>
    /// <param name="obj">操作对象</param>
    /// <param name="default_value">默认值</param>
    /// <param name="action">自定义转换函数,委托。</param>
    /// 委托函数如：delegate (object e)
    ///             {
    ///                 return e.ToString();
    ///             }
    /// <returns></returns>
    public static T? Parse<T>(this object? obj, T? default_value = default(T), Func<object, T>? action = null)
    {
        try
        {
            if (obj is null)
                return default_value;
            if (action is not null)
                return action(obj);

            var fo = default_value;
            fo = (T)Convert.ChangeType(obj, typeof(T));
            return fo;
        }
        catch
        {
            return default_value;
        }
    }

    /// <summary>
    /// 对象转json字符串
    /// </summary>
    /// <param name="obj">需要转换的对象</param>
    /// <param name="time_converter">json字符串时间格式处理：
    ///     如 new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" }
    /// </param>
    /// <returns></returns>
    public static string? ToJson(this object? obj, IsoDateTimeConverter? time_converter = null)
    {
        var json_obj = default(string);
        if (obj is null)
            return json_obj;        
        try
        {
            if(time_converter is null)
                time_converter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            json_obj = JsonConvert.SerializeObject(obj, time_converter);
        }
        catch
        {
        }
        return json_obj;
    }

    /// <summary>
    /// json字符串转对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="json"></param>
    /// <returns></returns>
    public static T? ToObject<T>(this string json)
    {
        var obj = default(T);
        if (string.IsNullOrEmpty(json))
            return obj;        
        try
        {
            obj = JsonConvert.DeserializeObject<T>(json);
        }
        catch
        {
            return obj;
        }
        return obj;
    }

    /// <summary>
    /// json字符串转集合
    /// </summary>
    /// <typeparam name="T">需要转换的类型</typeparam>
    /// <param name="json">操作的json字符串</param>
    /// <returns></returns>
    public static List<T>? ToList<T>(this string json)
    {
        var list = default(List<T>);
        if (string.IsNullOrEmpty(json))
            return list;        
        try
        {
            list = JsonConvert.DeserializeObject<List<T>>(json);
        }
        catch
        {
            return list;
        }
        return list;
    }
        
    /// <summary>
    /// json字符串转JObject
    /// </summary>
    /// <param name="json"></param>
    /// <returns></returns>
    public static JObject? ToJObject(this string json)
    {
        var obj = default(JObject);
        if (string.IsNullOrEmpty(json))
            return obj;
        try
        {
            obj = JsonConvert.DeserializeObject<JObject>(json.Replace("&nbsp;", ""));
        }
        catch
        {
            return obj;
        }
        return obj;
    }

    /// <summary>
    /// json字符串转JArray
    /// </summary>
    /// <param name="json"></param>
    /// <returns></returns>
    public static JArray? ToJArray(this string json)
    {
        var obj = default(JArray);
        if (string.IsNullOrEmpty(json))
            return obj;
        try
        {
            obj = JsonConvert.DeserializeObject<JArray>(json.Replace("&nbsp;", ""));
        }
        catch
        {
        }
        return obj;
    }

    /// <summary>
    /// 把字符串按照分隔符转换成 List
    /// </summary>
    /// <param name="str">源字符串</param>
    /// <param name="speater">分隔符</param>
    /// <param name="to_lower">是否转换为小写</param>
    /// <returns></returns>
    public static List<string> ToSplitList(this string str, char speater, bool to_lower)
    {
        List<string> list = new List<string>();
        if (string.IsNullOrEmpty(str) || string.IsNullOrWhiteSpace(str))
            return list;
        string[] ss = str.Split(speater);
        foreach (string s in ss)
        {
            if (!string.IsNullOrEmpty(s) && s != speater.ToString())
            {
                string strVal = s;
                if (to_lower)
                {
                    strVal = s.ToLower();
                }
                list.Add(strVal);
            }
        }
        return list;
    }


}